package com.yc.controller;


import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yc.Result;
import com.yc.entities.BloodSugarGluceProgram;
import com.yc.service.BloodSugarGluceProgramService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 血糖方案记录表 前端控制器
 * </p>
 *
 * @author kuakha
 * @since 2021-04-07
 */
@RestController
@RequestMapping("/bloodSugarGluceProgram")
@Api(value = "BloodSugarGluceController", tags = {"血糖测量计划相关接口"})
public class BloodSugarGluceProgramController {

    @Autowired
    private BloodSugarGluceProgramService bloodSugarGluceProgramService;

    /**
     * 查看用户所有的控糖计划
     *
     * @param userId
     * @return
     */
    @ApiOperation(value = "APP查看用户所有的控糖计划")
    @PostMapping("getSugarProgramByUser")
    public Result getSugarProgramByUser(String userId) {
        if (StringUtils.isEmpty(userId)) {
            return Result.error().message("用户Id不能为空").code(301);
        }
        QueryWrapper<BloodSugarGluceProgram> query = Wrappers.query();
        query.eq("program_gl_userid", userId);
        List<BloodSugarGluceProgram> list = bloodSugarGluceProgramService.list(query);
        if (CollectionUtil.isNotEmpty(list)) {
            return Result.ok().data(list);
        } else {
            return Result.ok().code(300).message("暂无数据");
        }
    }

    @ApiOperation(value = "APP修改用户血糖计划")
    @PostMapping("updateSugarProgramByUser")
    @Transactional
    public Result updateSugarProgramByUser(String userId, String sugarProgramJson) {
        int updateCount = 0;
        if (StringUtils.isEmpty(userId)) {
            return Result.error().message("用户Id不能为空").code(301);
        }
        if (StringUtils.isEmpty(sugarProgramJson)) {
            return Result.error().message("参数不全").code(302);
        }
        ArrayList<BloodSugarGluceProgram> bloodSugarGlucePrograms = new ArrayList<>();
        JSONArray objects = JSON.parseArray(sugarProgramJson);
        for (Object object : objects) {
            BloodSugarGluceProgram bloodSugarGluceProgram = JSONObject.parseObject(object.toString(), BloodSugarGluceProgram.class);
            bloodSugarGlucePrograms.add(bloodSugarGluceProgram);
        }
        for (BloodSugarGluceProgram bloodSugarGluceProgram : bloodSugarGlucePrograms) {
            UpdateWrapper<BloodSugarGluceProgram> updateWrapper = Wrappers.update();
            updateWrapper.eq("program_gl_attr", bloodSugarGluceProgram.getProgramGlAttr());
            updateWrapper.eq("program_gl_userid", bloodSugarGluceProgram.getProgramGlUserid());
            updateWrapper.set("program_gl_upvalue", bloodSugarGluceProgram.getProgramGlUpvalue());
            updateWrapper.set("program_gl_downvalue", bloodSugarGluceProgram.getProgramGlDownvalue());
            updateWrapper.set("program_gl_option", bloodSugarGluceProgram.getProgramGlOption());
            boolean update = bloodSugarGluceProgramService.update(updateWrapper);
            updateCount++;
        }
        if (updateCount == bloodSugarGlucePrograms.size()) {
            return Result.ok().message("操作成功");
        } else {
            return Result.error().message("操作失败");
        }
    }

    /**
     * 查看用户某个时间段的控糖计划
     *
     * @param userId 用户id
     * @param attr   时间段
     * @return
     */
    @ApiOperation(value = "APP查看用户某个时间段的控糖计划")
    @PostMapping("getSugarProgramByUserAndAttr")
    public Result getSugarProgramByUserAndAttr(String userId, String attr) {
        if (StringUtils.isEmpty(userId)) {
            return Result.error().message("用户Id不能为空").code(301);
        }
        if (StringUtils.isEmpty(attr)) {
            return Result.error().message("血糖测量时间点为空").code(302);
        }
        QueryWrapper<BloodSugarGluceProgram> query = Wrappers.query();
        query.eq("program_gl_userid", userId);
        query.eq("program_gl_attr", attr);
        List<BloodSugarGluceProgram> list = bloodSugarGluceProgramService.list(query);
        if (CollectionUtil.isNotEmpty(list)) {
            return Result.ok().data(list);
        } else {
            return Result.ok().code(300).message("暂无数据");
        }
    }

}